package com.jsjvm.server;

import java.io.IOException;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.jsjvm.client.JDWPPolling;
import com.jsjvm.server.dbgproxy.JDPABackEnd;
import com.jsjvm.shared.jdwp.events.JDWPEvent;
import com.jsjvm.shared.vmcmds.VMCommand;

@SuppressWarnings("serial")
public class JDWPPollImpl extends RemoteServiceServlet implements JDWPPolling {

	private static JDPABackEnd bkend;
	static {
		try {
			bkend = new JDPABackEnd(8000);
			bkend.handshake();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Send the events to the front-end, and let the backend wait for a new 
	 * packet (command) for the VM.
	 */
	@Override
	public VMCommand pollForCommands(JDWPEvent[] events) {
		try {
			bkend.sendEvents(events);
			return bkend.waitForCommand();
		} catch (IOException e) {
			return null; // backend error
		}
	}

}
